<?php
/*
 * @Description: 京东云短信类
 * @Author: QianLong
 * @Date: 2019-09-27 16:34:02
 * @LastEditors    : QianLong
 * @LastEditTime   : 2024-02-06 11:26:22
 */

namespace App\Service\Sms;

use App\Service\JDCloud;
use Yurun\Util\HttpRequest;

class JdSms
{
    /**
     * 接口请求
     */
    public function sendRequest($phone, $templateCode, $templateParam, $config, $sign_id)
    {
        if (!is_array($templateParam)) {
            $templateParam = json_decode($templateParam,true);
        }
        $regionId = 'cn-north-1';
        // $jdPost['regionId'] = 'cn-north-1';
        $jdPost['signId'] = $sign_id;
        $jdPost['templateId'] = $templateCode;
        $jdPost['params'] = $templateParam;
        $jdPost['phoneList'] = [strval($phone)];
        $jdCloud = new JDCloud($config, true);
        $result = $jdCloud->doRequest('https://sms.jdcloud-api.com/v1/regions/'. $regionId.'/batchSend', 'POST', 'sms', $regionId, $jdPost);
        // if (!empty($result) && isset($result['result']['data']['code']) && $result['result']['data']['code'] != '0000') {
        //     throw new ApiException(-1, json_encode($result, JSON_UNESCAPED_UNICODE));
        // }
        // {"result":{"code":11572106,"data":null,"message":"批量发送短信接口，套餐包余量不足","status":false},"requestId":"a662c384b18c45c6bfa346ae86374d6b"}
        $postData['send_result'] = ($result['result']['code'] == 200) ? true : false;
        $postData['send_error'] = ($result['result']['code'] != 200) ? $result['result']['message'] : '';
        $postData['send_result_data'] = json_encode($result, JSON_UNESCAPED_UNICODE);
        $postData['smsParam'] = json_encode($jdPost, JSON_UNESCAPED_UNICODE);
        return $postData;
    }
}
